package com.qr.web.controller.common;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.qr.common.constant.Constants;
import com.qr.common.core.domain.AjaxResult;
import com.qr.common.core.redis.RedisCache;
import com.qr.common.utils.StringUtils;
import com.qr.common.utils.poi.ExcelUtil;
import com.qr.system.domain.QrLogisticsTracking;
import com.qr.system.domain.QrPacking;
import com.qr.system.domain.QrWarehouse;
import com.qr.system.domain.dto.DemoData;
import com.qr.system.domain.tongtool.vo.GoodsReqVo;
import com.qr.system.domain.tongtool.vo.StocksReqVo;
import com.qr.system.domain.vo.QrLogisticsTrackingVo;
import com.qr.system.mapper.QrPackingMapper;
import com.qr.system.mapper.QrWarehouseMapper;
import com.qr.system.service.IQrLogisticsTrackingService;
import com.qr.system.service.ITongToolSerice;
import com.qr.web.enums.Color;
import com.qr.common.utils.TongtToolUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/tests")
public class testsController {

    @Autowired
    private TongtToolUtils tongtTuUtils;

    @Value("${tongtool.appSecret}")
    private String appSecret;
    private JSONObject parse;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IQrLogisticsTrackingService qrLogisticsTrackingService;

    @Autowired
    private ITongToolSerice iTongToolSerice;

    @Autowired
    private QrWarehouseMapper qrWarehouseMapper;

    @Autowired
    private QrPackingMapper qrPackingMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("test")
        public AjaxResult test(@RequestBody GoodsReqVo stocksReqVo) throws IOException {
        String str = "LEUK";
        String accountStr = str.substring(2);

        String pp = accountStr;

        QrPacking qrPacking = qrPackingMapper.selectShipmentIdgGroup("fsdfsdfsf");
        QrWarehouse gddd = qrWarehouseMapper.selectQrWarehouseByWarehouseId("gddd");

        QrPacking ttt = qrPacking;


        // iTongToolSerice.taskSyncFbaStock();
        // AjaxResult summaryData = qrLogisticsTrackingService.getSummaryData();

        // return summaryData;
        // redisCache.deleteObject("pageNo");

        JSONObject param = JSONObject.parseObject(JSON.toJSONString(stocksReqVo));
        JSONObject tt = param;
        String url = "/openapi/tongtool/goodsQuery";
        // String url = "/openapi/tongtool/warehouseQuery";
        String requeryTongtu = tongtTuUtils.getRequeryTongtu(url, param);
        JSONObject parse = JSONObject.parseObject(requeryTongtu);
        return AjaxResult.success(parse);

    }

    @RequestMapping("test2")
    public AjaxResult test2(@RequestBody StocksReqVo stocksReqVo) throws IOException {


        // AjaxResult summaryData = qrLogisticsTrackingService.getSummaryData();

        // return summaryData;
        // redisCache.deleteObject("pageNo");

        JSONObject param = JSONObject.parseObject(JSON.toJSONString(stocksReqVo));
        // String url = "/openapi/tongtool/goodsQuery";
        // String url = "/openapi/tongtool/stocksQuery";
        String url = "/openapi/tongtool/fbaStocksQuery";
        String requeryTongtu = tongtTuUtils.getRequeryTongtu(url, param);
        JSONObject parse = JSONObject.parseObject(requeryTongtu);
        return AjaxResult.success(parse);

    }

    @RequestMapping("test3")
    public void test3() {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三1");
        list.add("张三2");
        list.add("张三3");
        list.add("张三4");

        redisCache.setCacheList("tt_peng", list);


        // Color red = Color.RED;
        // red.printColor();

    }

    @RequestMapping("test4")
    public void test4() {

        String name = "DMUS";

        System.out.println(name.substring(2));

        // List<Object> fbaWarehouseList = redisCache.getCacheList("FBA_WAREHOUSE_NAME_LIST_KEY");
        // List<Object> ttd = fbaWarehouseList;
        // String value = String.valueOf(redisTemplate.opsForList().leftPop("tt_peng"));

        // Object ttPeng = redisCache.getCacheListIndex("tt_peng", 10);

        // Object tt = value;
        // Color red = Color.RED;
        // red.printColor();


    }

    @RequestMapping("test5")
    public List<String> test5() {
        // List<String> last12Months = new ArrayList<>();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // Calendar calendar = Calendar.getInstance();
        //
        // calendar.add(Calendar.MONTH, -12);
        // for (int i = 0; i < 12; i++) {
        //     calendar.add(Calendar.MONTH, 1);
        //     last12Months.add(sdf.format(calendar.getTime()));
        // }
        // return last12Months;
        return null;
    }

    public void test6(){

        // BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:/a.txt"));
        // bufferedWriter.write(JSON.toJSONString());
        // bufferedWriter.close();

    //     Workbook workbook = null;
    //     try {
    //         workbook = new XSSFWorkbook(file.getInputStream());
    //         // 获取第一个sheet页
    //         Sheet sheetAt = workbook.getSheetAt(0);
    //         // 获取表头
    //         Row row = sheetAt.getRow(0);
    //         List<String> headList = new ArrayList<>();
    //         for (Cell cell : row) {
    //             if (StringUtils.isNotEmpty(cell.getStringCellValue())) {
    //                 headList.add(cell.getStringCellValue());
    //             }
    //         }
    //         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:/a.txt"));
    //         bufferedWriter.write(JSON.toJSONString(headList));
    //         bufferedWriter.close();
    //
    //     } catch (Exception e) {
    //
    //
    //     } finally {
    //         if (workbook != null) {
    //             workbook.close();
    //         }
    //
    //     }
    //
    //     return null;
    // }

    }

    @PostMapping("test7")
    public void test7(MultipartFile file) throws IOException {
        long startTime = System.currentTimeMillis();

        DemoDataListener listener = new DemoDataListener();
        EasyExcel.read(file.getInputStream(), DemoData.class, listener).sheet().doRead();
        listener.getCachedDataList();
       long endTime = System.currentTimeMillis();
       long deff = (endTime - startTime)/1000;
       System.out.println("解析执行花了"+deff);

    }

    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws IOException {

        QrLogisticsTrackingVo qrLogisticsTracking = new QrLogisticsTrackingVo();
        List<QrLogisticsTracking> list = qrLogisticsTrackingService.selectQrLogisticsTrackingList(qrLogisticsTracking);
        ExcelUtil<QrLogisticsTracking> excelUtil = new ExcelUtil<QrLogisticsTracking>(QrLogisticsTracking.class);
        AjaxResult tt = excelUtil.exportExcel(list, "product");
        String msg = (String) tt.get("msg");
        String absoluteFile = excelUtil.getAbsoluteFile(msg);
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(absoluteFile.substring(absoluteFile.lastIndexOf("/") + 1), "UTF-8"));
        InputStream inputStream = new FileInputStream(new File(absoluteFile));
        byte data[] = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(data)) != -1) {
            response.getOutputStream().write(data, 0, len);
        }
    }


    public void t() {
        // long timestamp = System.currentTimeMillis();
        // System.out.println("我是时间戳："+ timestamp);
        // String app_token = "1501927731f729e1576dd14a986e7538";
        // StringBuffer stringBuffer = new StringBuffer();
        // String signStr = stringBuffer.append("app_token").append(app_token).append("timestamp").append(timestamp).append(this.appSecret).toString();
        // // 生成签名
        // String hash = Md5Utils.hash(signStr);
    }

}
